package be.gnx.fogo.application.ui;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import be.gnx.fogo.application.model.Address;
import be.gnx.fogo.application.model.Category;
import be.gnx.fogo.application.model.Communication;
import be.gnx.fogo.application.model.ExtraInfo;
import be.gnx.fogo.application.model.Givenname;
import be.gnx.fogo.application.model.Person;
import be.gnx.fogo.application.model.Sexe;

public class PersonFullDetail {

  private static char bornSymbol = '\u002A'; // or '\u2605'

  private static char diedSymbol = '\u2020';

  private static char marriedSymbol = '\u26AD'; // or '\u221E'

  private static char divorcedSymbol = '\u26AE'; // or '\u29DE'

  private static char engagedSymbol = '\u26AC';

  private Composite parentComposite;

  private Label idLabel;
  private Text idText;
  private Label alternativeNameLabel;
  private Text alternativeNameText;
  private Label surnameLabel;
  private Text surnameText;
  private Label sexeLabel;
  private Combo sexeCombo;

/*
  private Person spouse;

  private Person mother;

  private Person father;

  private Integer dateOfBirthYear;

  private Integer dateOfBirthMonth;

  private Integer dateOfBirthDay;

  private Integer dateOfDeathYear;

  private Integer dateOfDeathMonth;

  private Integer dateOfDeathDay;

  private Set<Category> categorySet = new HashSet<Category>();

  private Set<Communication> communicationSet = new HashSet<Communication>();

  private List<Givenname> givennameList = new ArrayList<Givenname>();

  private Set<ExtraInfo> extraInfoSet = new HashSet<ExtraInfo>();

  private Set<Person> fatherChildren = new HashSet<Person>();

  private Set<Person> motherChildren = new HashSet<Person>();
*/
  private Text birthText;

  private Text deathText;

  private Image personImage;


  
  private Text addressText;
  private Text telHomeText;
  private Text telWorkText;
  private Text telMobileText;
  private Text emailHomeText;
  private Text emailWorkText;

  private Button saveButton;

  private Vector<Label> labels = new Vector<Label>();

  private Vector<Control> controls = new Vector<Control>();

  public PersonFullDetail(Composite parentComposite, ResourceBundle resourceBundle) {
    this.parentComposite = parentComposite;

    idLabel = new Label(parentComposite, SWT.RIGHT);
    idLabel.setText("id");
    labels.add(idLabel);
    idText = new Text(parentComposite, SWT.READ_ONLY | SWT.LEFT | SWT.SINGLE);
    controls.add(idText);

    alternativeNameLabel = new Label(parentComposite, SWT.RIGHT);
    alternativeNameLabel.setText("roepnaam");
    labels.add(alternativeNameLabel);
    alternativeNameText = new Text(parentComposite, SWT.NONE);
    controls.add(alternativeNameText);

    surnameLabel = new Label(parentComposite, SWT.RIGHT);
    surnameLabel.setText("naam");
    labels.add(surnameLabel);
    surnameText = new Text(parentComposite, SWT.NONE);
    controls.add(surnameText);

    sexeLabel = new Label(parentComposite, SWT.RIGHT);
    sexeLabel.setText("geslacht");
    labels.add(sexeLabel);
    sexeCombo = new Combo(parentComposite, SWT.NONE);
    sexeCombo.setItems(new String[] { Sexe.FEMALE.getValue(),  Sexe.MALE.getValue(), Sexe.UNKNOWN.getValue()});
    controls.add(sexeCombo);

    addressText = new Text(parentComposite, SWT.NONE);
    telHomeText = new Text(parentComposite, SWT.NONE);
    telWorkText = new Text(parentComposite, SWT.NONE);
    telMobileText = new Text(parentComposite, SWT.NONE);
    emailHomeText = new Text(parentComposite, SWT.NONE);
    emailWorkText = new Text(parentComposite, SWT.NONE);

    saveButton = new Button(parentComposite, SWT.PUSH);
    saveButton.setText("save");
    resize();
  }

  protected void resize() {
    int labelMaxWidth = 0;
    int labelMaxHeight = 0;
    for (Label label : labels) {
      Point labelSize = label.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
      if (labelSize.x > labelMaxWidth) {
        labelMaxWidth = labelSize.x;
      }
      if (labelSize.y > labelMaxHeight) {
        labelMaxHeight = labelSize.y;
      }
    }

    int controlMaxWidth = 0;
    int controlMaxHeight = 0;
    for (Control control : controls) {
      Point controlSize = control.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
      if (controlSize.x > controlMaxWidth) {
        controlMaxWidth = controlSize.x;
      }
      if (controlSize.y > controlMaxHeight) {
        controlMaxHeight = controlSize.y;
      }
    }

    int labelIndex = 0;
    for (Label label : labels) {
      label.setBounds(0, Math.max(labelMaxHeight, controlMaxHeight) * labelIndex, labelMaxWidth, Math.max(labelMaxHeight, controlMaxHeight));
      labelIndex++;
    }

    int controlIndex = 0;
    for (Control control : controls) {
      control.setBounds(labelMaxWidth, Math.max(labelMaxHeight, controlMaxHeight) * controlIndex, controlMaxWidth, Math.max(labelMaxHeight, controlMaxHeight));
      controlIndex++;
    }

    int margin = 2;
    Point saveButtonSize = saveButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
    saveButton.setBounds(parentComposite.getBounds().width - (saveButtonSize.x + margin), parentComposite.getBounds().height - (saveButtonSize.y + margin), saveButtonSize.x, saveButtonSize.y);
  }

  protected void setPerson(Person person) {
    if (person != null) {
      idText.setText(person.getId().toString());
      alternativeNameText.setText(person.getAlternativeName().getValue());
      surnameText.setText(person.getSurname().getValue());
      sexeCombo.setText(person.getSexe().getValue());

      Address address = null;
      String phoneHome = "";
      String phoneWork = "";
      String phoneMobile = "";
      String phoneFax = "";
      String emailHome = "";
      String emailWork = "";
      if ((person.getCommunicationSet() != null) && (person.getCommunicationSet().size() > 0)) {
        for (Communication communication : person.getCommunicationSet()) {
          if (communication.getCategory().getName().equals("Primary Personal Address")) {
            address = communication.getAddress();
          }
          if (communication.getCategory().getName().equals("Primary Personal E-Mail")) {
            emailHome = communication.getString() + emailHome;
          }
          if (communication.getCategory().getName().equals("Auxiliary Personal E-Mail")) {
            emailHome += " " + communication.getString();
          }
          if (communication.getCategory().getName().equals("Primary Business E-Mail")) {
            emailWork = communication.getString() + emailWork;
          }
          if (communication.getCategory().getName().equals("Auxiliary Business E-Mail")) {
            emailWork += " " + communication.getString();
          }
          if (communication.getCategory().getName().equals("Primary Personal Phone Number")) {
            phoneHome = communication.getNumber() + phoneHome;
          }
          if (communication.getCategory().getName().equals("Auxiliary Personal Phone Number")) {
            phoneHome += " " + communication.getNumber();
          }
          if (communication.getCategory().getName().equals("Primary Business Phone Number")) {
            phoneWork = communication.getNumber() + phoneWork;
          }
          if (communication.getCategory().getName().equals("Auxiliary Business Phone Number")) {
            phoneWork += " " + communication.getNumber();
          }
          if (communication.getCategory().getName().equals("Primary Personal Mobile Number")) {
            phoneMobile = communication.getNumber() + phoneMobile;
          }
          if (communication.getCategory().getName().equals("Auxiliary Personal Mobile Number")) {
            phoneMobile += " " + communication.getNumber();
          }
          if (communication.getCategory().getName().equals("Primary Personal Fax Number")) {
            phoneFax = communication.getNumber() + phoneFax;
          }
          if (communication.getCategory().getName().equals("Auxiliary Personal Fax Number")) {
            phoneFax += " " + communication.getNumber();
          }
        }
      }

      addressText.setText(address.getStreetNumberBus() + "," + address.getPostcodeCity() + "," + address.getCountry().getIso_3166_1_alpha2_as_string());
      if (phoneHome != null) {
        telHomeText.setText(phoneHome);
      }
      if (phoneWork != null) {
        telWorkText.setText(phoneWork);
      }
      if (phoneMobile != null) {
        telMobileText.setText(phoneMobile);
      }
      if (emailHome != null) {
        emailHomeText.setText(emailHome);
      }
      if (emailWork != null) {
        emailWorkText.setText(emailWork);
      }
    }
  }

}
